package com.njuse_czf.kg.web.service.impl.handler;

import com.njuse_czf.kg.web.service.KGGraphService;
import java.util.ArrayList;
import java.util.List;

public class HeaderHandler extends AbstractQuestionHandler{

    private List<AbstractQuestionHandler> handlerList;

    private AbstractQuestionHandler header;

    public HeaderHandler(){
        handlerList = new ArrayList<>();
        header = this;
    }

    @Override
    public void handleQuestion(QuestionCtx questionCtx, String question, KGGraphService kgGraphService) {
        if(handlerList != null || !handlerList.isEmpty()) {
            handlerList.get(0).handleQuestion(questionCtx, question, kgGraphService);
        }
    }

    public HandlersBuilder builder(){
        return new HandlersBuilder();
    }


    public class HandlersBuilder {
        public HandlersBuilder addHandler(AbstractQuestionHandler handler) {
            handlerList.add(handler);
            return this;
        }

        public AbstractQuestionHandler build() {
            AbstractQuestionHandler current = header;
            for(AbstractQuestionHandler handler : handlerList){
                current.setNextHandler(handler);
                current = handler;
            }
            current.setNextHandler(new BackStopHandler());

            return header;
        }
    }
}
